package com.subfg.common.util.call;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.subfg.common.config.RestTempateConfig;
import com.subfg.common.entity.common.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Configuration
@EnableScheduling
public class RestTemplateUtil {
    private Logger log = LoggerFactory.getLogger(RestTemplateUtil.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RestTempateConfig restTempateConfig;




    // 有Data 返回值调用   支持http 和 https 双重
    public <T>T remoteCallPostCommonResult(String url, String pageName, Object param, Class<?> clazz) throws Exception {
        log.info("本次远程调用接口为==>>> {}, 调用者为==>>> {}", url, pageName);
        ResponseEntity<String> callResult = restTempateConfig.restTemplate(restTempateConfig.httpComponentsClientHttpRequestFactory()).postForEntity(url, param, String.class);
        //ResponseEntity<String> callResult = restTemplate.postForEntity(url, param, String.class);
        if (callResult.getStatusCodeValue() != 200) {
            log.error("调用远程接口错误!!! 调用接口为==>>>{}错误状态码===>{},调用具体错误如下==>>>\n{}", url, callResult.getStatusCodeValue(), callResult.getBody());
            throw new Exception();
        }
        CommonResult commonResult = JSONObject.parseObject(callResult.getBody(), CommonResult.class);
        if (commonResult.getCode() != 200) {
            log.error("远程接口返回异常!!! 调用接口为==>>>{}错误状态码===>{}, 具体返回参数如下==>>>\n{}", url, commonResult.getCode(), JSONObject.toJSON(commonResult.getData()));
            throw new Exception();
        }
        return (T) new ObjectMapper().convertValue(commonResult.getData(), clazz);
    }


    // CommonResult返回 空 Data, code 为 200 表示操作成功
    public boolean remoteCallPost(String url, String pageName, Object param) throws Exception {
        log.info("本次远程调用接口为==>>> {}, 调用者为==>>> {}", url, pageName);
        ResponseEntity<String> callResult = restTempateConfig.restTemplate(restTempateConfig.httpComponentsClientHttpRequestFactory()).postForEntity(url, param, String.class);
        if (callResult.getStatusCodeValue() != 200) {
            log.error("调用远程接口错误!!! 调用接口为==>>>{}错误状态码===>{},调用具体错误如下==>>>\n{}", url, callResult.getStatusCodeValue(), callResult.getBody());
            throw new Exception();
        }
        CommonResult commonResult = JSONObject.parseObject(callResult.getBody(), CommonResult.class);
        if (commonResult.getCode() != 200) {
            log.error("远程接口返回异常!!! 调用接口为==>>>{}错误状态码===>{}, 具体返回参数如下==>>>\n{}", url, commonResult.getCode(), JSONObject.toJSON(commonResult.getCode()));
            throw new Exception();
        }
        return true;
    }
}
